<?php
require_once( 'mofachi.ContentData.php' );
abstract class Content {
	protected $contentData;
	protected $is_loaded;
	protected static $tableID;
	protected static $ContentDataType;
	protected static $ContentType;

	public function __get( $key ) {
		return $this->contentData->$key;
	}
	public function __set( $key, $value ) {
		switch( $key ) {
			case static::$tableID:
				if( !$this->is_loaded ) {
					$id = static::$tableID;
					$this->contentData->$id = $value;
					break;
				}
			case "issue_time":
			case "edit_time":
			case "vote_yes":
			case "vote_no":
				echo "--cannot set ".$key;
				break;
			default:
				$this->contentData->$key = $value;
		}
	}
	public function isLoaded() {
		return $this->is_loaded;
	}
	public function loadByID() {
		$id = static::$tableID;
		if( !$this->$id ) {
			return false;
		}		
		$this->is_loaded = $this->contentData->loadByID();
		return $this->is_loaded;
	}
	public function voteYes() {
		return $this->contentData->vote( 1, 0 );
	}
	public function voteNo() {
		return $this->contentData->vote( 0, 1 );
	}
	public function remove() {
		$id = static::$tableID;
		if( !$this->$id ) {
			return false;
		}
		$result = $this->contentData->remove();
		if( $result )
			unset( $this->contentData );
		return $result;
	}
	//-------------------------------------------------------------------------------------
	// static functions
	//-------------------------------------------------------------------------------------	
	public static function getRecent( $offset, $length ) {
		$offset = ( isset( $offset ) ) ? $offset : 0;
		$length = ( isset( $length ) ) ? $length : 20;

		$dataType = static::$ContentDataType;
		$type = static::$ContentType;

		$data = $dataType::getRecent( $offset, $length );
		$contents = array();
		foreach( $data as $cd ) {
			$c = new $type();
			$c->contentData = $cd;
			array_push( $contents, $c );
		}
		return $contents;
	}
}
?>